﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace CtxPublishTool
{
    public class DelegateCommand : ICommand
    {
        #region Ctors
        public DelegateCommand(Action<object> executeFunc)
            : this(executeFunc, obj => true)
        {
        }

        public DelegateCommand(Action<object> executeFunc, Func<object, bool> canExecuteFunc)
        {
            ExecuteFunc = executeFunc;
            CanExecuteFunc = canExecuteFunc;
        }
        #endregion

        #region Properties
        public Func<object, bool> CanExecuteFunc { get; private set; }
        public Action<object> ExecuteFunc { get; private set; }

        #endregion

        #region Implement Interfaces
        public bool CanExecute(object parameter)
        {
            if (CanExecuteFunc != null)
                return CanExecuteFunc(parameter);
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (CanExecute(parameter) && ExecuteFunc != null)
            {
                ExecuteFunc(parameter);
            }
        }

        #endregion
    }
}
